//
//  MessageManager.swift
//  demo3
//
//  Created by hanweiguang on 2019/1/3.
//  Copyright © 2019年 hanweiguang. All rights reserved.
//

import UIKit
import Starscream

class MessageMamager : NSObject {
    
    
    static let  shared = MessageMamager()
    var socket : WebSocket!
    
    override init() {
        super.init()
        self.socket = WebSocket(url: URL(string: "ws://192.168.1.212:28082/user?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xOTIuMTY4LjEuMjEyOjI4MDgyXC9mYXZpY29uLmljbyIsImlhdCI6MTU0NjUxOTA0NywiZXhwIjoxNTQ2NTIyNjQ3LCJuYmYiOjE1NDY1MTkwNDcsImp0aSI6IjVTMFpPUUU0NHdXSUFlTGkiLCJzdWIiOjMyMywicHJ2IjoiNjRlNDkzYzcwMTRiZjZkNGM4Yzg1YWQzNDkyNmFkZTYzODZjMGE3OCJ9.s2Iqq5hS5OH831aoyYLgqPMR0JKYq7ImqK3dpAh_dgM")!)
        self.socket.delegate = self
        
    }
 
}


extension MessageMamager : WebSocketDelegate {
    
    func websocketDidConnect(socket: WebSocketClient) {
        
        print("连接成功")
    }
    
    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
                print("连接断开")
    }
    
    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
                  print("收到消息\(text)")
        //收到消息推送给 weex
        var weexInstance = WeexMap.shared.dictMap["homea"]
        if weexInstance != nil
        {
            
            print("存在")
            let dictinoary:[String:String] = ["key1":"value1", "key2":"value"]
            
            let weex:WXSDKInstance = weexInstance as! WXSDKInstance
            weex.viewController.title = "f你好改"
            
                weex.fireGlobalEvent("update",params: dictinoary)
        }
        
        
   
        
    }
    
    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
        
    }
 
    
    
    
}
